// pages/order/order.js
const $api = require('./../../utils/request').API;
const filter = require('../../utils/loginCheck');
Page(filter.loginCheck({


  data: {
    code: '',
    active: null,
    info: {},
    list: [
      { price: 0.01, give: 0, name: '会员卡', type: 'H' },
      { price: 2000, give: 500, name: '铂金卡', type: 'B' },
      { price: 1000, give: 200, name: '贵宾卡', type: 'G' },
      { price: 20000, give: 1000, name: '钻石卡', type: 'S' },
    ]

  },

  choseItem(e) {
    console.log(e.currentTarget.dataset.item);
    // console.log(e.currentTarget.dataset.index);
    this.setData({
      active: e.currentTarget.dataset.index,
      info: e.currentTarget.dataset.item,
    })
  },

  getInputValue(e) {
    this.setData({
      code: e.detail.value,
    })

  },

  chongzhi() {
    if (this.data.active == null) {
      return wx.showToast({
        title: '请选择充值额度',
        icon: 'error'
      })
    } else {
      const { price, give } = this.data.info;
      let params = {
        amount: price + give,
        openId: wx.getStorageSync('openid'),
        shishouAmount: price,
        code: this.data.code
      }
      wx.showLoading({
        title: '正在支付',
      })
      $api.rechargeOrder(params).then(res => {
        console.log(res.data.weiXinPayResponse)
wx.hideLoading()
        wx.requestPayment(
          {
            "timeStamp": res.data.weiXinPayResponse.miniPayRequest.timeStamp,
            "nonceStr": res.data.weiXinPayResponse.miniPayRequest.nonceStr,
            "package": res.data.weiXinPayResponse.miniPayRequest.pack,
            "signType": res.data.weiXinPayResponse.miniPayRequest.signType,  
            "paySign": res.data.weiXinPayResponse.miniPayRequest.paySign,  
            "success": function (res) {

                wx.showToast({
                  title: '支付成功',
                  icon:"success"
                })
             },
            "fail": function (res) {
              wx.showToast({
                title: '支付失败',
                icon:"error"
              })
             },
            "complete": function (res) { }
          })


      }).catch(err => {
        wx.hideLoading()
        return wx.showToast({
          title: '充值失败',
          icon: 'error'
        })
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      $api.getpayList().then(res=>{
        console.log(res);
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))